import request from "./request";

/**
 * @description 未登录用户可以获取有效期1分钟的临时token，仅用于调用登录接口
 */
export const getToken = () => {
  return request("/auth/token", "get");
};

/** 登录 */
export const login = (data, token) => {
  return request("/auth/h5login", "post", data);
};

/** 退出登录 */
export const logout = () => {
  return request("/auth/logout", "get");
};

/** 获取用户信息 */
export const getUserInfo = () => {
  return new Promise((resolve, reject) => {
    request("/user/info", "get")
      .then((res) => {
        const { data } = res;
        if (data && Object.keys(data).length > 0) {
          uni.setStorageSync("userInfo", data);
          resolve(data);
        } else {
          reject(res);
        }
      })
      .catch((err) => {
        console.log("获取用户信息失败：", err);
        reject(err);
      });
  });
};

/** 获取用户权限信息 */
export const getUserPermission = () => {
  return new Promise((resolve, reject) => {
    request("/user/perms", "get")
      .then((res) => {
        const { data } = res;
        if (data && Object.keys(data).length > 0) {
          uni.setStorageSync("userPermission", data);
          resolve(data);
        } else {
          reject(res);
        }
      })
      .catch((err) => {});
  });
};

/** 获取当前用户机构列表 */
export const getUserOrgList = () => {
  return request("/user/orgs", "get");
};

/** 重置用户密码 */
export const resetUserPassword = (data) => {
  return request("/sys/user/resetPwd", "put", data);
};
